var app = getApp()
var util = require('../../md5.js');
Page({

  data: {
    userName: '',
    userTelephone: '',
    userEnterprisee:'',
    sendtime:'',
    userid:'',
    needPay1:'',
    userQuestion:'',
    successInfo:{}
  },
  onLoad: function(options) {
    var timeList = options.time.split(",");
    var arr=[];
    for (var i in timeList) {
      arr.push(timeList[i]);
      console.log(arr + 'timearr')
    }
    this.setData({
      sendtime:arr,
      userid: options.id,
      needPay1: options.needPay
    })
  
    wx.showLoading({
      title: '请稍候',
    })
  },
  q: function () {
    wx.navigateBack({ changed: true })
  },
  qq: function () {
    wx.switchTab({

      url: '/pages/index/index',

    });
  },
  onReady: function () {
    const vm = this
    vm.setData({
      statusBarHeight: getApp().globalData.statusBarHeight,
      titleBarHeight: getApp().globalData.titleBarHeight
    })
    wx.hideLoading()
  },
 
  send:function(){
    var that = this
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/Consult/order_add',
      data: {
        types: 0,
        bespeak_time: this.data.sendtime,
        name: this.data.userName,
        phone: this.data.userTelephone,
        company: this.data.userEnterprisee,
        problem: this.data.userQuestion,
        ser_id: this.data.userid,
        needPay1: this.data.needPay,
        myId: app.globalData.myId
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
          console.log(res)
        console.log(res.data.data)
        if (res.data.status === 1) {
          that.getPrePayId(res.data.data.id)
          
          that.setData({
            successInfo: res.data.data
          })
         
         
        } else {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '请求失败',
            showCancel: false
          })
        }

      },
      fail: function () {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '请求失败',
          showCancel: false
        })
      }


    })
  },

  getPrePayId: function (oid) {
    console.log(oid)
    console.log(app.globalData.myId)
    var that = this
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/WeiPayZixunXcx/prePayProcess',
      data: {
        oid: oid,
        myId: app.globalData.myId,
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)

        if (res.data.status === 1) {
          that.sendPayMent(res.data.nonce_str, res.data.prepay_id)
         
        }
        else {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '请求失败',
            showCancel: false
          })
        }
      }, fail: function (e) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '请求失败',
          showCancel: false
        })
      }

    })
  },

  sendPayMent: function (nonce_str, prepay_id) {
    var that = this
    var time = Date.parse(new Date()) / 1000;
    var paySign = util.hexMD5('appId=wxd59c11f9decb5e58&nonceStr=' + nonce_str + '&package=prepay_id=' + prepay_id + '&signType=MD5&timeStamp=' + time + '&key=lkjmkluhjjm3256jkk52188iojkklok5')

    wx.requestPayment({
      'timeStamp': time + '',
      'nonceStr': nonce_str,
      'package': 'prepay_id=' + prepay_id,
      'signType': 'MD5',
      'trade_type':'JSAPI',
      'paySign': paySign,
      success: function (res) {
        console.log(res)

        if (res.errMsg == 'requestPayment:ok') {

          wx.showToast({
            title: '支付成功',
          }),
          wx.navigateTo({
            url: '../makeSuccess/makeSuccess?successInfo='+JSON.stringify(that.data.successInfo),
          })
                   
        } else {

        }

      },
      'fail': function (res) {
        console.log('fail')
        console.log(res)
      }
    })
  },
  userNameInput: function(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  userTelephone: function(e) {
    this.setData({
      userTelephone: e.detail.value
    })
  },
  userEnterprise: function (e) {
    this.setData({
      userEnterprisee: e.detail.value
    })
  },
  userQuestion: function (e) {
    this.setData({
      userQuestion: e.detail.value
    })
  },
  toMakeSuccess: function () {
    wx.navigateTo({
      url: '../makeSuccess/makeSuccess',
    })
  },
})